﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyVoteMVC.BackstageManagement.Controllers;
using MyVoteMVC.Domain;


namespace MyVoteMVC.BackstageManagement.Views
{
    public partial class ImportActivityToItem : Form,IItemView
    {
        public event EventHandler OnAddComplete;

        private string questionId;
        private string activityId;
        private string parentActivityId;

        public IItemController controller;

        public ImportActivityToItem(string activityId, string parentActivityId, string questionId)
        {
            InitializeComponent();
            controller = new ItemController(this);
            this.questionId = questionId;
            this.activityId = activityId;
            this.parentActivityId = parentActivityId;
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void AddItem_FormClosing(object sender, FormClosingEventArgs e)
        {
            OnAddComplete(null, null);
        }

        public void CreateItemComplete(string message)
        {
            MessageBox.Show(message);
        }

        public void EditItemComplete(string message)
        {
            throw new NotImplementedException();
        }

        public void GetItemComplete(VT_Items item)
        {
            throw new NotImplementedException();
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            if (gvItem.SelectedRows.Count > 0)
            {
                List<VT_Items> itemsToAdd = new List<VT_Items>();
                foreach (DataGridViewRow row in gvItem.SelectedRows)
                {
                    VT_Items oldItem = row.DataBoundItem as VT_Items;
                    VT_Items newItem = new VT_Items();
                    //string newid = Guid.NewGuid().ToString();
                    //newItem.Item_ID = newid;
                    newItem.Question_ID = questionId;
                    newItem.Item_Title = oldItem.Item_Title;
                    newItem.Item_Content = oldItem.Item_Content;
                    newItem.Item_IfDefault = 0;
                    newItem.Project_ID = oldItem.Project_ID;
                    newItem.ItemImage = new VT_ItemImage()
                    {
                        //Item_ID = newid,
                        Item_Image = ((oldItem.ItemImage != null && oldItem.ItemImage.Item_Image != null) ? oldItem.ItemImage.Item_Image : null)
                    };

                    itemsToAdd.Add(newItem);
                }
                controller.CreateItems(itemsToAdd);
            }
            else
            {
            }
        }

        public void GetProjectsComplete(IList<VT_Projects> projects)
        {
            throw new NotImplementedException();
        }

        private void AddItem_Load(object sender, EventArgs e)
        {
            controller.GetItemsBySecond(parentActivityId);
        }

        public void OnSubmitError(string message)
        {
            MessageBox.Show(message);
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtKeyWord.Text))
            {
                string kw = txtKeyWord.Text;
                controller.GetItemsBySecond(parentActivityId,kw);
                
                //IList<VT_Items> items = gvItem.DataSource as List<VT_Items>;
                //var searchlist = items
                //    .Where(o => o.Item_Title.Contains(kw) 
                //        || (o.Project != null && (o.Project.Project_Type.Contains(kw) || o.Project.Project_Year.Contains(kw)))).ToList();
                //gvItem.DataSource = searchlist;
            } 
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            gvItem.SelectAll();
        }


        public void CreateItemsComplete(string message)
        {
            MessageBox.Show(message);
        }

        public void GetItemsComplete(IList<VT_Items> item)
        {
            gvItem.DataSource = item;
        }

        private void txtKeyWord_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
